

var userid=sessionStorage.getItem("userid");
toconfigure()
function toconfigure() {
    $("#configurationtable").html("<thead>\n" +
        "                    <tr >\n" +
        "                        <th width='100'>模块</th>\n" +
        "                        <th width='100'>内容</th>\n" +
        "                        <th width='100'>操作</th>\n" +
        "                    </tr>\n" +
        "                    </thead>\n" +
        "                    <tbody class=\"shun\">\n" +
        "                    </tbody>");
    $.ajax({
        type: "get",
        url:'https://'+ window.location.host +'/api/admin/arights/userDataConfig/getInfo/'+userid+'',
        dataType: "json",
        data: {
        },
        //传入组装的参数
        success: function (data) {
            if(data.errorCode=="0") {
                console.log()
                var handStr=""
                //发货仓
                var cc=""
                cc='<a onclick=""><span onclick="warehouseCodeadd()">确认</span></a>&nbsp;&nbsp;<a onclick="warehouseCodeReset()">重置</a>'
                var deliveryWarehouse=""
                var sysUserAreas=data.data.sysUserAreas
                if(sysUserAreas!=""&&sysUserAreas!=null){
                    deliveryWarehouse+='<label><input type="checkbox"  onclick="allElection()" id="warehouseCodes">全选</label>&nbsp;&nbsp;'
                    for(var i = 0; i < sysUserAreas.length; i++) {
                        if(sysUserAreas[i].isContain==true){
                            deliveryWarehouse+='<label><input type="checkbox" checked class="warehouseCodesid" name="warehouseCode" value=' + sysUserAreas[i].warehouseCode + ' >' + sysUserAreas[i].warehouseName + '</label>&nbsp;&nbsp;'

                        }else {
                            deliveryWarehouse+='<label><input type="checkbox" class="warehouseCodesid" name="warehouseCode" value=' + sysUserAreas[i].warehouseCode + ' >' + sysUserAreas[i].warehouseName + '</label>&nbsp;&nbsp;'

                        }

                    }

                }



                //公司内容
                var dataInter2=data.data.sysUserOrganizations;
                var integralValues2=[]
                for (var i = 0; i < dataInter2.length; i++)
                {
                    integralValues2.push(dataInter2[i].organizationName);   //调用数组的push方法将数据添加到数组中
                }
                var company= integralValues2.join(";")

                //人员内容
                var dataInter3=data.data.sysUserSalesSupervisors;
                var integralValues3=[]
                for (var i = 0; i < dataInter3.length; i++)
                {
                    integralValues3.push(dataInter3[i].salesSupervisorUserName);   //调用数组的push方法将数据添加到数组中
                }
                var personnel= integralValues3.join(";")
                var aa=""
                if(data.data.sysUserOrganizations==""){
                aa='<a onclick="configure(\'公司配置\',\'company-configuration.html\')"><span >编辑</span></a>&nbsp;&nbsp;<a style="opacity: 0.2"><span >重置</span></a>'
                }else {
                    aa='<a onclick="configure(\'公司配置\',\'company-configuration.html\')"><span >编辑</span></a>&nbsp;&nbsp;<a onclick="delestesj()">重置</a>'

                }


                var bb=""
                if(data.data.sysUserOrganizations==""&&data.data.sysUserSalesSupervisors==""){
                    bb='<a style="opacity: 0.2"><span >编辑</span></a>&nbsp;&nbsp;<a style="opacity: 0.2"><span >重置</span></a>'
                }else if(data.data.sysUserOrganizations!=""&&data.data.sysUserSalesSupervisors==""){
                    bb='<a onclick="configure(\'人员配置\',\'staffing.html\')"><span >编辑</span></a>&nbsp;&nbsp;<a style="opacity: 0.2"><span >重置</span></a>'
                }else {
                    bb='<a onclick="configure(\'人员配置\',\'staffing.html\')"><span >编辑</span></a>&nbsp;&nbsp;<a onclick="deletesj()">重置</a>'

                }
                handStr+=
                    '<tr>'+
                    '<td>发货仓</td>'+
                    '<td><span id="company">'+deliveryWarehouse+'</span></td>'+
                    '<td>'+cc+'</td>'+
                    '</tr>'+
                    '<tr>'+
                    '<td><span class="c-red">*</span>公司配置</td>'+
                    '<td><span id="company">'+company+'</span></td>'+
                    '<td>'+aa+'</td>'+
                    '</tr>'+
                    '<tr>'+
                    '<td>人员配置</td>'+
                    '<td><span id="personnel">'+personnel+'</span></td>'+
                    '<td>'+bb+'</td>'+
                    '</tr>';
                $("#configurationtable").find("tbody").append($(handStr));
                zidong()
            }
        }
    })
}

//全选
function allElection() {
    if($("#warehouseCodes").is(':checked')==true){
        //全选
        //市
        var isclick =$(".warehouseCodesid")
        console.log($(".warehouseCodesid"))
        for(var i = 0; i < isclick.length; i++) {
            isclick[i].checked = true;
        }

    }else {
        //全不选
        var isclick =$(".warehouseCodesid")
        for(var i = 0; i < isclick.length; i++) {
            isclick[i].checked = false;
        }

    }


}
//判断全选复选框是否选中
function zidong() {
    var warehouseCodesid =$(".warehouseCodesid")
    var warehouseCodesidall =$(".warehouseCodesid:checked")
    console.log($(".warehouseCodesid:checked"))
    if(warehouseCodesidall.length==warehouseCodesid.length){
        $("#warehouseCodes").prop("checked", true)
    }else {
        $("#warehouseCodes").prop("checked", false)
    }
}
//发货仓保存
function warehouseCodeadd() {
     var warehouseCodeArr = [];
     var warehouseCodeall={}
    var warehouseCodeid =  $(".warehouseCodesid");
    for (var i = 0; i < warehouseCodeid.length; i++) {
        if(warehouseCodeid[i].checked){
            warehouseCodeall={
                "warehouseCode":warehouseCodeid[i].value
            }
            warehouseCodeArr.push(warehouseCodeall)
        }

    }
    $.ajax({
        url:'https://'+ window.location.host +'/api/admin/arights/userDataConfig/saveWarehouseConfig',
        cache: false,  //禁用缓存
        type: "POST",
        dataType:'json',
        contentType: "application/json",
        data: JSON.stringify({
            "userId":userid,
            "configInfos":warehouseCodeArr

        }),
        success: function (data) {
            if(data.errorCode==0){
                $.Huimodalalert('成功',2000);
                toconfigure()
            }else {
                $.Huimodalalert(data.msg,2000);
            }
        },
        error: function () {
            $.Huimodalalert('失败！',2000);
        }
    });
}
//发货仓重置
function warehouseCodeReset() {
    $.ajax({
        type: "post",
        url:'https://'+ window.location.host +'/api/admin/arights/userDataConfig/deleteWarehouseConfig',
        cache: false,  //禁用缓存
        dataType: "json",
        data: {
            "userId":userid
        },
        //传入组装的参数
        success: function (data) {
            toconfigure()
        }
    })
}


function deletesj() {
    $.ajax({
        type: "post",
        url:'https://'+ window.location.host +'/api/admin/arights/userDataConfig/deleteSalesSupervisorConfig',
        cache: false,  //禁用缓存
        dataType: "json",
        data: {
            "userId":userid
        },
        //传入组装的参数
        success: function (data) {
            toconfigure()
        }
    })
}
function delestesj() {
    $.ajax({
        type: "post",
        url:'https://'+ window.location.host +'/api/admin/arights/userDataConfig/deleteOrganizationConfig',
        cache: false,  //禁用缓存
        dataType: "json",
        data: {
            "userId":userid
        },
        //传入组装的参数
        success: function (data) {
            toconfigure()
        }
    })
}

function configure(title,url) {
    sessionStorage.setItem("userids",userid);
    var index = layer.open({
        type: 2,
        title: title,
        content: url
    });
    layer.full(index);
}